#!/bin/bash
set -e
# 源码地址，需使用 SSH 协议，并提前配置好公钥
SSH_GIT_REPO="git@gitee.com:$GITEE_REPO.git"
# 源码目录
SOURCE_DIR=$1
# 使用的分支
BRANCH=$GITEE_BRANCH

if [ ! -d "$SOURCE_DIR" ]; then
  echo "mkdir: $SOURCE_DIR"
  mkdir -p "$SOURCE_DIR"

  echo "git clone: $SSH_GIT_REPO"
  git clone --depth=1 "$SSH_GIT_REPO" "$SOURCE_DIR"
fi
cd "$SOURCE_DIR" || exit
git checkout .
git fetch origin
if ! git branch --list "$BRANCH" >/dev/null; then
  echo "checkout: $BRANCH"
  git checkout -b "$BRANCH" origin/"$BRANCH"
fi
git checkout "$BRANCH"
git pull